package com.ruoyi.mall.service;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUserUtils;
import com.ruoyi.mall.domain.MallShoppingCart;
import com.ruoyi.mall.mapper.MallShoppingCartMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 购物车Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-11
 */
@Service
public class MallShoppingCartService {
    @Autowired
    private MallShoppingCartMapper mallShoppingCartMapper;

    /**
     * 查询购物车
     *
     * @param cartId 购物车主键
     * @return 购物车
     */
    public MallShoppingCart selectMallShoppingCartByCartId(Long cartId) {
        return mallShoppingCartMapper.selectMallShoppingCartByCartId(cartId);
    }

    /**
     * 查询购物车列表
     *
     * @param mallShoppingCart 购物车
     * @return 购物车
     */
    public List<MallShoppingCart> selectMallShoppingCartList(MallShoppingCart mallShoppingCart) {
        return mallShoppingCartMapper.selectMallShoppingCartList(mallShoppingCart);
    }

    /**
     * 加入购物车
     *
     * @param mallShoppingCart 购物车
     * @return 结果
     */
    public int insertMallShoppingCart(MallShoppingCart mallShoppingCart) {
        // 查询用户、商品、规格是否有购物车记录，有则增加数量，否则添加记录
        MallShoppingCart cart = mallShoppingCartMapper.selectByUser(mallShoppingCart.getUserId(), mallShoppingCart.getGoodsId(), mallShoppingCart.getNormId());
        if (cart == null) {
            // 新增
            mallShoppingCart.setCreateTime(DateUtils.getNowDate());
            return mallShoppingCartMapper.insertMallShoppingCart(mallShoppingCart);
        } else {
            // 把此次加入购物车的数量加到已存在的购物车记录上
            cart.setNum(cart.getNum() + mallShoppingCart.getNum());
            cart.setUpdateTime(DateUtils.getNowDate());
            return mallShoppingCartMapper.updateMallShoppingCart(cart);
        }
    }

    /**
     * 修改购物车
     *
     * @param mallShoppingCart 购物车
     * @return 结果
     */
    public int updateMallShoppingCart(MallShoppingCart mallShoppingCart) {
        // 修改购物车，判断是否用户的购物车记录
        MallShoppingCart cart = mallShoppingCartMapper.selectMallShoppingCartByCartId(mallShoppingCart.getCartId());
        if (ObjectUtils.notEqual(cart.getUserId(), SecurityUserUtils.getUserId())) {
            throw new ServiceException("无权限操作");
        }
        // 只能修改数量
        cart.setNum(mallShoppingCart.getNum());
        cart.setUpdateTime(DateUtils.getNowDate());
        return mallShoppingCartMapper.updateMallShoppingCart(cart);
    }

    /**
     * 批量删除购物车
     *
     * @param cartIds 需要删除的购物车主键
     * @return 结果
     */
    public int deleteMallShoppingCartByCartIds(Long[] cartIds) {
        long userId = SecurityUserUtils.getUserId();
        // 校验地址是否为当前用户
        List<MallShoppingCart> list = mallShoppingCartMapper.selectByIds(Arrays.asList(cartIds));
        boolean hasOtherUser = list.stream().anyMatch(userAddress -> !userAddress.getUserId().equals(userId));
        if (hasOtherUser) {
            throw new ServiceException("删除的购物车记录不是当前用户，无法删除");
        }
        return mallShoppingCartMapper.deleteMallShoppingCartByCartIds(cartIds);
    }
}
